package com.xhrd.framework.service;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.xhrd.framework.dao.BaseDAO;
import com.xhrd.framework.dao.domain.BaseEntity;
import com.xhrd.framework.utils.GenericsUtils;
import com.xhrd.framework.vo.Page;
import com.xhrd.framework.vo.SearchCondition;

@SuppressWarnings(value = { "unchecked" })
@Transactional
public class EntityServiceImpl<Entity extends BaseEntity, SC extends SearchCondition> extends BaseServiceImpl implements EntityService<Entity, SC> {

    /**
     * 日志对象
     */
    protected Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * DAO
     */
    protected BaseDAO<Entity, Serializable> dao = null;

    public void setDao(BaseDAO<Entity, Serializable> dao) {
        this.dao = dao;
    }

    @Override
    public Entity get(String id) {
        return dao.findById(id);
    }

    @Override
    public List<Entity> get(Serializable... ids) {
        return dao.findByIds(ids);
    }

    @Override
    public Entity save(Entity entity) {
        return dao.save(entity);
    }

    @Override
    public Entity update(Entity entity) {
        return dao.update(entity);
    }

    @Override
    public List<Entity> search(SearchCondition searchCondition) {
        return dao.search(searchCondition);
    }

    @Override
    public Entity searchUnique(SearchCondition searchCondition) {
        return dao.searchUnique(searchCondition);
    }

    @Override
    public List<Entity> getAll() {
        return dao.findAll();
    }

    @Override
    public boolean remove(Serializable id) {
        return dao.removeById(id);
    }

    @Override
    public boolean remove(Serializable... ids) {
        return dao.removeByIds(ids);
    }

    @Override
    public boolean remove(Entity entity) {
        return dao.remove(entity);
    }

    @Override
    public boolean logicRemove(Serializable id) {
        Class<Entity> entityClass = GenericsUtils.getGenericClass(getClass());
        try {
            Entity entity = entityClass.newInstance();
            entity.setStatus("-1");
            entity.setId(id);
            return dao.remove(entity);
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Page<Entity> searchPage(SearchCondition searchCondition) {
        return dao.pageQuery("selectPage", searchCondition);
    }

    @Override
    public boolean saveOrUpdate(Entity entity) {
        if (this.saveOrUpdateJudge(entity)) {
            update(entity);
            return true;
        } else {
            save(entity);
            return false;
        }
    }

    public boolean saveOrUpdateJudge(Entity entity) {
        if (entity != null && StringUtils.hasText(entity.getId().toString())) {
            return true;
        }
        return false;
    }

}
